package com.jiangds.open.pattern;

import java.util.HashMap;
import java.util.regex.Pattern;

/**
 * {@link PatternFactory}  正则表达式的缓存池,防止每次对正则的编译
 *
 * @author jiangliang
 * @since 2021/1/5 15:18
 */
public class PatternFactory {

    private static HashMap<String, Pattern> patternMap = new HashMap<>(16);

    public synchronized static Pattern get(String regex) {
        Pattern pattern = patternMap.get(regex);
        if (null == pattern) {
            pattern = Pattern.compile(regex);
            patternMap.put(regex, pattern);
        }
        return pattern;
    }
}
